package com.wnx.job.modules.ums.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.domain.CascadeNode;
import com.wnx.job.domain.DictNode;
import com.wnx.job.modules.ums.mapper.UmsDictMapper;
import com.wnx.job.modules.ums.model.UmsDict;
import com.wnx.job.modules.ums.service.UmsDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class UmsDictServiceImpl extends ServiceImpl<UmsDictMapper, UmsDict> implements UmsDictService {

    @Resource
    private UmsDictMapper dictMapper;

    /**
     * 获取城市树
     * @return
     */
    @Override
    public List<CascadeNode> getCityTree() {

        List<UmsDict> dictList = this.findDictListByTypeId(7L);

        //转为树形结构
        List<DictNode> result = dictList.stream()
                .filter(dict -> dict.getParentId().equals(0L))
                .map(dict -> buildTreeDict(dict, dictList))
                .collect(Collectors.toList());
        //实体类转Dto
        return convert(result);
    }

    /**
     * 根据字典类型ID查询字典集合
     * @param typeId
     * @return
     */
    @Override
    public List<UmsDict> findDictListByTypeId(long typeId) {
        return dictMapper.selectList(new QueryWrapper<UmsDict>().eq("type_id",typeId));
    }

    /**
     *  分页查询字典
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<UmsDict> findByPage(QueryPageBean queryPageBean) {
        List<UmsDict> container = new ArrayList<>();
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Integer typeId = (Integer)  queryPageBean.getQueryParams().get("typeId");
        if (typeId!=null){
            container = baseMapper.selectList(new QueryWrapper<UmsDict>().lambda().eq(UmsDict::getTypeId,typeId));
        }else {
            container = baseMapper.selectList(null);
        }
        return PageResult.restPage(container);
    }

    /**
     * 实体转DTO
     * @param dictTree
     * @return
     */
    private List<CascadeNode> convert(List<DictNode> dictTree) {

        List<CascadeNode> dictVo = new ArrayList<>();
        dictTree.forEach(m -> {
            CascadeNode vo = new CascadeNode();
            vo.setLabel(m.getDictVal());
            vo.setValue(m.getId());
            List<DictNode> sonDictList = m.getChildren();
            if (sonDictList != null && sonDictList.size() > 0) {//是
                vo.setChildren(convert(sonDictList));
            }
            dictVo.add(vo);
        });
        return dictVo;
    }


    /**
     * 将权限转换为带有子级的权限对象
     * 当找不到子级权限的时候map操作不会再递归调用covert
     */
    private DictNode buildTreeDict(UmsDict dict, List<UmsDict> dictList) {
        DictNode node = new DictNode();
        BeanUtils.copyProperties(dict, node);
        List<DictNode> children = dictList.stream()
                .filter(subDict -> subDict.getParentId().equals(dict.getId()))
                .map(subPermission -> buildTreeDict(subPermission, dictList))
                .collect(Collectors.toList());

        node.setChildren(children);

        return node;
    }
}
